/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-09-26
 * Time: 11:26
 */
public class sum_Numbers {
    public static int sumNumbers(TreeNode root) {
        return sumNumber(root, "");
    }

    public static int sumNumber(TreeNode root, String ret) {
        if (root.left == null&&root.right==null) {
            System.out.println(ret);
            return Integer.parseInt(ret+root.val);
        }

        int left = 0;
        if (root.left != null) {
            left = sumNumber(root.left, ret + root.val);
        }

        int right = 0;
        if (root.right != null) {
            right = sumNumber(root.right, ret + root.val);
        }

        return left + right;
    }

    public static void main(String[] args) {
        TreeNode head1 = new TreeNode(1);
        TreeNode head2 = new TreeNode(2);
        TreeNode head3 = new TreeNode(3);
        head1.left = head2;
        head1.right = head3;
        System.out.println(sumNumbers(head1));
    }

}
